package com.lcc.generate.util;

public class JDBCUtil {

    //驼峰转下划线
    public static String toLine(String str) {
        return str.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    //下划线转驼峰
    public static String toHump(String str){
        return toHump(str, false);
    }

    /**
     * 转驼峰
     * @param str
     * @param uppercase 首字母大写
     * @return
     * @author lcc
     * @date 2024/12/30 9:56
     */
    public static String toHump(String str,boolean uppercase) {
        int _c = str.indexOf("_");
        int first = _c;
        if (_c < 1 && !uppercase) return str;
        StringBuilder sb = new StringBuilder(str);
        while (_c != -1) {
            char ch = sb.charAt(_c + 1);
            if (ch >= 'a' && ch <= 'z') ch -= 32;
            sb.setCharAt(_c + 1, ch);
            sb.deleteCharAt(_c);
            _c = sb.indexOf("_", _c);
        }
        // 首字母大写 除了第二个字母是下划线
        if(uppercase && first != 1){
            char ch = sb.charAt(_c + 1);
            if(ch >= 'a' && ch <= 'z'){
                ch -= 32;
                sb.setCharAt(0,ch);
            }
        }
        return sb.toString();
    }

    public static String ifToHump(boolean is, String str,boolean uppercase) {
        if(is){
            return toHump(str,uppercase);
        }else if(uppercase){
            return (str.charAt(0)+"").toUpperCase() + str.substring(1);
        }else{
            return str;
        }
    }

}